<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../../../css/mui.min.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/pages/share.css" />
	</head>

	<body>
		<div id="app" class="issue">
			<div class="details">
				<textarea rows="5" cols="2" v-model="dynDetails" placeholder="这一刻的想法..."></textarea>
			</div>
			<!--上传图片-->
			<div class="img-wapper m-t-15">
				<div class="preview" v-for="(item,index) in listImg" v-if="listImg.length > 0">
					<i class="delete" @click="deleteClick(index)"></i>
					<img :src="item" @click="getItemImgClick(index)">
				</div>
				<div v-if="listImg.length!=9" class="button" @click="addImgClick">
					<img src="../../../images/add.png">
				</div>
			</div>
			<div class="botton-wapper" @click="issueClick">发布</div>
		</div>
		<script src="../../../js/Width.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../../js/vue.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../../js/Util.js" type="text/javascript" charset="utf-8"></script>
		<script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
		<script type="text/javascript">
			mui.init()
			mui.plusReady(function() {
				if (mui.os.ios) {
					FastClick.attach(document.body);
					plus.webview.currentWebview().setStyle({
						softinputMode: "adjustResize" // 弹出软键盘时自动改变webview的高度
					});
				}
			})

		

			var app = new Vue({
				el: "#app",
				data: {
					listImg: [],
					dynDetails: ''
				},
				methods: {
					// 				add: function () {
					// 					var that = this
					// 					if (mui.os.plus) {
					// 						var a = [{
					// 							title: "上传图片"
					// 						}, {
					// 							title: "上传视频"
					// 						}];
					// 						plus.nativeUI.actionSheet({
					// 							cancel: "取消",
					// 							buttons: a
					// 						}, function (b) {
					// 							switch (b.index) {
					// 								case 0:
					// 									break;
					// 								case 1:
					// 									that.addImgClick()
					// 									break;
					// 								case 2:
					// 									that.addVideo();
					// 									break;
					// 								default:
					// 									break
					// 							}
					// 						})
					// 					}
					// 				},

					addImgClick: function() {
						var that = this
						// 从相册中选择图片
						mui.toast('最多选择9张');
						plus.gallery.pick(function(e) {
							that.listImg = e.files
						}, function(e) {
							mui.toast('取消选择图片');
						}, {
							filter: 'image',
							multiple: true,
							maximum: 9,
							system: false,
							onmaxed: function() {
								plus.nativeUI.alert('最多只能选择9张图片');
							}
						});
					},
					// 				addVideo: function () {
					// 					plus.gallery.pick(function (path) {
					// 						console.log(path);
					// 						player = plus.video.createVideoPlayer('videoplayer', {
					// 							src: path,
					// 							top: '0',
					// 							left: '0',
					// 							width: '100%',
					// 							height: '100%',
					// 							position: 'static',
					// 							controls: true,
					// 							poster: '../../../images/add.png'
					// 						});
					// 						plus.webview.currentWebview().append(player);
					// 						player.show();
					// 					}, function (e) {
					// 						mui.toast("取消选择视频");
					// 					}, {
					// 						filter: "video"
					// 					});
					// 				},

					deleteClick: function(num) {
						this.listImg.splice(num, 1)
					},

					getItemImgClick: function(num) {
						var listImg = this.listImg
						plus.nativeUI.previewImage(listImg, {
							current: num
						})
					},
					issueClick: function() {
						if (!this.dynDetails) {
							mui.toast('请填写完整')
							return
						}
						this.PostDynamics(this.dynDetails)
					},
					PostDynamics: function(dynDetails) {
						var that = this;
						mui.post(Util.HTTP + 'api/Dynamics/PostDynamics', {
							userID: Util.getState('$state').userID,
							dynDetails: dynDetails
						}, function(res) {
							if (res.code == 1001) {
								if (that.listImg.length > 0) {
									that.ActivityImg(res.data, function(seta) {
										// callback上传回调事件
										if (seta) {
											mui.toast('发布成功')
										} else {
											mui.toast('上传图片失败，未能发布')
										}
										setTimeout(function() {
											mui.back()
											var list = plus.webview.currentWebview().opener();
											//触发父页面的自定义事件(refresh),从而进行刷新
											mui.fire(list, 'issueBack');
											//返回true,继续页面关闭逻辑
											return true;
										}, 1000)
									})
								} else {
									mui.toast('发布成功')
									setTimeout(function() {
										mui.back()
										var list = plus.webview.currentWebview().opener();
										//触发父页面的自定义事件(refresh),从而进行刷新
										mui.fire(list, 'issueBack');
										//返回true,继续页面关闭逻辑
										return true;
									}, 1000)
								}
							} else {
								mui.toast(res.message)
							}
						})
					},
					ActivityImg: function(filesBizID, callback) { // 图片方法 调用上图片接口
						var that = this
						var server = Util.HTTP + 'api/Dynamics/DynamicsImg';
						var imgList = that.listImg;
						var wt = plus.nativeUI.showWaiting();
						var task = plus.uploader.createUpload(server, {
								method: "POST"
							},
							function(t, status) { //上传完成
								if (status == 200) {
									wt.close(); //关闭等待提示按钮
									callback(true) // 上传成功回调事件
								} else {
									alert("上传失败：" + status);
									wt.close(); //关闭等待提示按钮
									callback(false) // 上传失败回调事件
								}
							}
						);
						//添加其他参数
						task.addData("filesBizID", filesBizID);
						imgList.forEach(function(item, num) {
							task.addFile(item, {
								key: item
							});
						})
						task.start();
					},
				}
			})
		</script>
	</body>

</html>
